package solutions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-28
 * Time: 14:05
 */

//https://leetcode.cn/problems/search-insert-position/submissions/397484643/?envType=study-plan&id=suan-fa-ru-men&plan=algorithms&plan_progress=bmyvagt
class Solution6 {
    public int searchInsert(int[] nums, int target) {
        //有序数组二分查找
        int left = 0;
        int right = nums.length - 1 ;
        int mid = 0;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] >= target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        //left即是找到下标或插入位置
        return left;
    }
}
